package com.eqdd.yiqidian.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * Created by lv on 17-9-7.
 */

public class PushCodeRefreshService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("PushCodeRefreshService->onStartCommand");
        refreshCode();
        return super.onStartCommand(intent, flags, startId);
    }

    private void refreshCode() {
        if (AppConstent.user != null) {
            OkGo.<HttpResult<List<ServerCode>>>post(HttpConfig.BASE_URL + HttpConfig.GET_ALL_PUSHCODE)
                    .params("userGuid", AppConstent.user.getGuid())
                    .execute(new JsonCallBack<HttpResult<List<ServerCode>>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<List<ServerCode>>> response) {
                            HttpResult<List<ServerCode>> httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                for (ServerCode serverCode : httpResult.getItems()) {
                                    GreenDaoUtil.setNum(serverCode.code, serverCode.count);
                                    stopSelf();
                                }
                            }
                        }
                    });
        }
    }

    class ServerCode {

        /**
         * code : 23
         * count : 1
         */

        private int code;
        private int count;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }
    }
}
